[[...path]].page.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import React from 'react';
  2. import { IUser, IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { useTranslation } from 'next-i18next';
  7. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  8. import dynamic from 'next/dynamic';
  9. import CountBadge from '~/components/Common/CountBadge';
  10. import PageListIcon from '~/components/Icons/PageListIcon';
  11. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  12. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  13. import { Page } from '~/components/Page';
  14. import styles from '~/components/Page/DisplaySwitcher.module.scss'; // for PageList toc style
  15. import TableOfContents from '~/components/TableOfContents';
  16. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  17. import { CrowiRequest } from '~/interfaces/crowi-request';
  18. import { RendererConfig } from '~/interfaces/services/renderer';
  19. import { IShareLinkHasId } from '~/interfaces/share-link';
  20. import {
  21. useCurrentUser, useCurrentPagePath, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  22. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault,
  23. } from '~/stores/context';
  24. import { useDescendantsPageListModal } from '~/stores/modal';
  25. import loggerFactory from '~/utils/logger';
  26. import {
  27. CommonProps, getServerSideCommonProps, useCustomTitle, getNextI18NextConfig,
  28. } from '../utils/commons';
  29. const logger = loggerFactory('growi:next-page:share');
  30. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  31. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  32. type Props = CommonProps & {
  33. shareLink?: IShareLinkHasId,
  34. isExpired: boolean,
  35. currentUser: IUser,
  36. disableLinkSharing: boolean,
  37. isSearchServiceConfigured: boolean,
  38. isSearchServiceReachable: boolean,
  39. isSearchScopeChildrenAsDefault: boolean,
  40. rendererConfig: RendererConfig,
  41. };
  42. const SharedPage: NextPage<Props> = (props: Props) => {
  43. useIsSearchPage(false);
  44. useShareLinkId(props.shareLink?._id);
  45. useCurrentPageId(props.shareLink?.relatedPage._id);
  46. useCurrentPagePath(props.shareLink?.relatedPage.path);
  47. useCurrentUser(props.currentUser);
  48. useCurrentPathname(props.currentPathname);
  49. useRendererConfig(props.rendererConfig);
  50. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  51. useIsSearchServiceReachable(props.isSearchServiceReachable);
  52. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  53. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  54. const { t } = useTranslation();
  55. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  56. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  57. const shareLink = props.shareLink;
  58. return (
  59. <ShareLinkLayout title={useCustomTitle(props, 'GROWI')} expandContainer={props.isContainerFluid}>
  60. <div className="h-100 d-flex flex-column justify-content-between">
  61. <header className="py-0 position-relative">
  62. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  63. </header>
  64. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  65. <div className="flex-grow-1">
  66. <div id="content-main" className="content-main">
  67. <div className="grw-container-convertible">
  68. { props.disableLinkSharing && (
  69. <div className="mt-4">
  70. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  71. </div>
  72. )}
  73. { (isNotFound && !props.disableLinkSharing) && (
  74. <div className="container-lg">
  75. <h2 className="text-muted mt-4">
  76. <i className="icon-ban" aria-hidden="true" />
  77. <span> Page is not found</span>
  78. </h2>
  79. </div>
  80. )}
  81. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  82. <div className="container-lg">
  83. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  84. <h2 className="text-muted mt-4">
  85. <i className="icon-ban" aria-hidden="true" />
  86. <span> Page is expired</span>
  87. </h2>
  88. </div>
  89. )}
  90. {(isShowSharedPage && shareLink != null) && (
  91. <>
  92. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  93. <div className="d-flex flex-column flex-lg-row-reverse">
  94. <div className="grw-side-contents-container">
  95. <div className="grw-side-contents-sticky-container">
  96. {/* Page list */}
  97. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  98. { shareLink.relatedPage.path != null && (
  99. <button
  100. type="button"
  101. className="btn btn-block btn-outline-secondary grw-btn-page-accessories
  102. rounded-pill d-flex justify-content-between align-items-center"
  103. onClick={() => openDescendantPageListModal(shareLink.relatedPage.path)}
  104. data-testid="pageListButton"
  105. >
  106. <div className="grw-page-accessories-control-icon">
  107. <PageListIcon />
  108. </div>
  109. {t('page_list')}
  110. <CountBadge count={shareLink.relatedPage.descendantCount} offset={1} />
  111. </button>
  112. ) }
  113. </div>
  114. <div className="d-none d-lg-block">
  115. <TableOfContents />
  116. </div>
  117. </div>
  118. </div>
  119. <div className="flex-grow-1 flex-basis-0 mw-0">
  120. <Page />
  121. </div>
  122. </div>
  123. </>
  124. )}
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. </ShareLinkLayout>
  130. );
  131. };
  132. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  133. const req: CrowiRequest = context.req as CrowiRequest;
  134. const { crowi } = req;
  135. props.disableLinkSharing = crowi.configManager.getConfig('crowi', 'security:disableLinkSharing');
  136. props.isSearchServiceConfigured = crowi.searchService.isConfigured;
  137. props.isSearchServiceReachable = crowi.searchService.isReachable;
  138. props.isSearchScopeChildrenAsDefault = crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  139. props.rendererConfig = {
  140. isEnabledLinebreaks: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  141. isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  142. adminPreferredIndentSize: crowi.configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  143. isIndentSizeForced: crowi.configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  144. plantumlUri: process.env.PLANTUML_URI ?? null,
  145. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  146. // XSS Options
  147. isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  148. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  149. tagWhiteList: crowi.xssService.getTagWhiteList(),
  150. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  151. };
  152. }
  153. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  154. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  155. props._nextI18Next = nextI18NextConfig._nextI18Next;
  156. }
  157. function getAction(props: Props): SupportedActionType {
  158. let action: SupportedActionType;
  159. if (props.isExpired) {
  160. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  161. }
  162. else if (props.shareLink == null) {
  163. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  164. }
  165. else {
  166. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  167. }
  168. return action;
  169. }
  170. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  171. const req: CrowiRequest = context.req as CrowiRequest;
  172. const parameters = {
  173. ip: req.ip,
  174. endpoint: req.originalUrl,
  175. action,
  176. user: req.user?._id,
  177. snapshot: {
  178. username: req.user?.username,
  179. },
  180. };
  181. await req.crowi.activityService.createActivity(parameters);
  182. }
  183. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  184. const req = context.req as CrowiRequest<IUserHasId & any>;
  185. const { user, crowi, params } = req;
  186. const result = await getServerSideCommonProps(context);
  187. if (!('props' in result)) {
  188. throw new Error('invalid getSSP result');
  189. }
  190. const props: Props = result.props as Props;
  191. if (user != null) {
  192. props.currentUser = user.toObject();
  193. }
  194. try {
  195. const ShareLinkModel = crowi.model('ShareLink');
  196. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  197. if (shareLink != null) {
  198. props.isExpired = shareLink.isExpired();
  199. props.shareLink = shareLink.toObject();
  200. }
  201. }
  202. catch (err) {
  203. logger.error(err);
  204. }
  205. injectServerConfigurations(context, props);
  206. await injectNextI18NextConfigurations(context, props);
  207. await addActivity(context, getAction(props));
  208. return {
  209. props,
  210. };
  211. };
  212. export default SharedPage;